/**
 * @description 首页属性组件
 */
import React, { useEffect, useMemo, useState, useContext, useRef } from 'react'
import { Collapse, Form, Select, Input, Radio, Tooltip } from 'antd';
import {
  InfoCircleOutlined
} from '@ant-design/icons';


import { setAttrForm } from '@/utils/AttrForm';
import AnimateOptions from '@/components/StylesWrapper/AnimateOptions.js';
import { AntIConTip, AntUrl, iconTextReg } from '@/utils/Const';
import { UpdateContext } from '@/store/updateContext';



import FileUpload from '@/components/StylesWrapper/FileUpload';
import CssEdit from '@/components/Monaco/CssEidt';
import AttrHeaderBtn from '../components/AttrHeaderBtn';


import StyleModule from '@/utils/StyleModule';
import { copyTemplate, pasteTemplate_2026 } from '@/utils/clipboard';

// 标题和后缀现实的开关
const titleShowOptions = [
  {
    label: '开',
    value: true
  },
  {
    label: '关',
    value: false
  }
]


function HomeAttr({ template2, onConfigEvent, setIsRightAttr, actKey }) {

  const updateCtx = useContext(UpdateContext)
  const [form] = Form.useForm()
  // css编辑器
  const [open, setOpen] = useState(false)
  // css编辑器实例
  const cssEditRef = useRef()



  // 组件css变化
  let cssChange = (value) => {
    form.setFieldValue('css', value)
    StyleModule.update(actKey, value)
  }

  // 图标输入框变化事件，比普通输入框，多一个字符串转化逻辑
  let iconEvent = (keys, value) => {

    // 去除 空格、<、/> 
    let newValue = value.replace(iconTextReg, '')
    form.setFieldValue(keys, newValue)

    onConfigEvent(keys, newValue)
  }

  // 头部按钮事件
  let handleHeaderEvent = (key) => {
    switch (key) {

      // 打开组件css编辑
      case 'css':
        setOpen(true)
        break;

      // 复制模板
      case 'copy':
        copyTemplate(actKey, [], form)
        break;

      // 黏贴模板
      case 'paste':
        pasteTemplate_2026(form, onConfigEvent, actKey)
        updateCtx.setUpdateFlag(pre => !pre)

        // 修改css编辑器的值
        cssEditRef.current.setValue(form.getFieldValue('css'))
        break;

      // 切换位置
      case 'position':
        setIsRightAttr((isRight) => !isRight)
        break;

      default:
        break;
    }
  }

  // -----------------------------------
  // 构造折叠面板的数据
  // -----------------------------------


  let items = useMemo(() => {
    let items = [

      {
        key: 'notice',
        label: '公告',
        children:
          <>
            <Form.Item
              label="开关"
              name={['base', 'notice_flag']}
            >
              <Radio.Group
                options={titleShowOptions}
                onChange={(e) => onConfigEvent(['base', 'notice_flag'], e.target.value)}
                optionType="button"
                buttonStyle="solid"
              />
            </Form.Item>

            <Form.Item
              label="文本"
              name={['base', 'notice_text']}
            >
              <Input
                onChange={(e) => onConfigEvent(['base', 'notice_text'], e.target.value)}
              />
            </Form.Item>

          </>
      },
      {
        key: 'title',
        label: '标题',
        children:
          <>
            <Form.Item
              label="盒子开关"
              name={['base', 'title_flag']}
            >
              <Radio.Group
                options={titleShowOptions}
                onChange={(e) => onConfigEvent(['base', 'title_flag'], e.target.value)}
                optionType="button"
                buttonStyle="solid"
              />
            </Form.Item>


            <Form.Item
              label="动画属性"
              name={['base', 'title_animate']}
            >
              <Select
                allowClear
                options={AnimateOptions}
                onChange={(value) => onConfigEvent(['base', 'title_animate'], value)}
              />
            </Form.Item>

            <Form.Item
              label="文本开关"
              name={['base', 'title_textFlag']}
            >
              <Radio.Group
                options={titleShowOptions}
                onChange={(e) => onConfigEvent(['base', 'title_textFlag'], e.target.value)}
                optionType="button"
                buttonStyle="solid"
              />
            </Form.Item>

            <Form.Item
              label="图片开关"
              name={['base', 'title_img_flag']}
            >
              <Radio.Group
                options={titleShowOptions}
                onChange={(e) => onConfigEvent(['base', 'title_img_flag'], e.target.value)}
                optionType="button"
                buttonStyle="solid"
              />

            </Form.Item>

            <FileUpload form={form} label='图片' name={['base', 'title_img_url']} onConfigEvent={onConfigEvent} />



          </>
      },
      {
        key: 'static',
        label: '统计',
        children:
          <Collapse accordion items={[
            {
              key: 'animates',
              label: '动画属性',
              children:
                <>
                  <Form.Item
                    label="统计1"
                    name={['base', 'static_animate1']}
                  >
                    <Select
                      allowClear
                      options={AnimateOptions}
                      onChange={(value) => onConfigEvent(['base', 'static_animate1'], value)}
                    />
                  </Form.Item>
                  <Form.Item
                    label="统计2"
                    name={['base', 'static_animate2']}
                  >
                    <Select
                      allowClear
                      options={AnimateOptions}
                      onChange={(value) => onConfigEvent(['base', 'static_animate2'], value)}
                    />
                  </Form.Item>
                  <Form.Item
                    label="统计3"
                    name={['base', 'static_animate3']}
                  >
                    <Select
                      allowClear
                      options={AnimateOptions}
                      onChange={(value) => onConfigEvent(['base', 'static_animate3'], value)}
                    />
                  </Form.Item>
                  <Form.Item
                    label="统计4"
                    name={['base', 'static_animate4']}
                  >
                    <Select
                      allowClear
                      options={AnimateOptions}
                      onChange={(value) => onConfigEvent(['base', 'static_animate4'], value)}
                    />
                  </Form.Item>
                  <Form.Item
                    label="统计5"
                    name={['base', 'static_animate5']}
                  >
                    <Select
                      allowClear
                      options={AnimateOptions}
                      onChange={(value) => onConfigEvent(['base', 'static_animate5'], value)}
                    />
                  </Form.Item>


                </>
            },
            {
              key: 'icons',
              label: 'icon图标',
              children:
                <>
                  <Form.Item
                    label="统计1"
                    name={['base', 'static_icon1']}
                  >
                    <Input
                      onChange={(e) => iconEvent(['base', 'static_icon1'], e.target.value)}
                      suffix={
                        <Tooltip title={AntIConTip}>
                          <InfoCircleOutlined
                            style={{
                              color: 'rgba(0,0,0,.45)',
                            }}
                            onClick={() => window.open(AntUrl, "_blank")}
                          />
                        </Tooltip>
                      }
                    />
                  </Form.Item>
                  <Form.Item
                    label="统计2"
                    name={['base', 'static_icon2']}
                  >
                    <Input
                      onChange={(e) => iconEvent(['base', 'static_icon2'], e.target.value)}
                    />
                  </Form.Item>
                  <Form.Item
                    label="统计3"
                    name={['base', 'static_icon3']}
                  >
                    <Input
                      onChange={(e) => iconEvent(['base', 'static_icon3'], e.target.value)}
                    />
                  </Form.Item>
                  <Form.Item
                    label="统计4"
                    name={['base', 'static_icon4']}
                  >
                    <Input
                      onChange={(e) => iconEvent(['base', 'static_icon4'], e.target.value)}
                    />
                  </Form.Item>
                  <Form.Item
                    label="统计5"
                    name={['base', 'static_icon5']}
                  >
                    <Input
                      onChange={(e) => iconEvent(['base', 'static_icon5'], e.target.value)}
                    />
                  </Form.Item>
                </>
            },
          ]} />


      },
      {
        key: 'calendar',
        label: '日历',
        children:
          <>
            <Form.Item
              label="动画"
              name={['base', 'calendar_animate']}
            >
              <Select
                allowClear
                options={AnimateOptions}
                onChange={(value) => onConfigEvent(['base', 'calendar_animate'], value)}
              />
            </Form.Item>


          </>

      },
      {
        key: 'chart',
        label: '图表',
        children:
          <>
            <Form.Item
              label="图表1"
              name={['base', 'chart_animate1']}
            >
              <Select
                allowClear
                options={AnimateOptions}
                onChange={(value) => onConfigEvent(['base', 'chart_animate1'], value)}
              />
            </Form.Item>
            <Form.Item
              label="图表2"
              name={['base', 'chart_animate2']}
            >
              <Select
                allowClear
                options={AnimateOptions}
                onChange={(value) => onConfigEvent(['base', 'chart_animate2'], value)}
              />
            </Form.Item>
            <Form.Item
              label="图表3"
              name={['base', 'chart_animate3']}
            >
              <Select
                allowClear
                options={AnimateOptions}
                onChange={(value) => onConfigEvent(['base', 'chart_animate3'], value)}
              />
            </Form.Item>
            <Form.Item
              label="图表4"
              name={['base', 'chart_animate4']}
            >
              <Select
                allowClear
                options={AnimateOptions}
                onChange={(value) => onConfigEvent(['base', 'chart_animate4'], value)}
              />
            </Form.Item>
            <Form.Item
              label="图表5"
              name={['base', 'chart_animate5']}
            >
              <Select
                allowClear
                options={AnimateOptions}
                onChange={(value) => onConfigEvent(['base', 'chart_animate5'], value)}
              />
            </Form.Item>


          </>
      }
    ]
    return items
  }, [
    updateCtx.updateFlag
  ])


  useEffect(() => {
    setAttrForm(form)
  }, [form])

  return (
    <>

      <Form
        name='home'
        form={form}
        initialValues={template2}
        labelCol={{
          span: 6,
        }
        }
        autoComplete="off"
        spellCheck="false"
      >

        {/* 按钮区域 */}
        <AttrHeaderBtn onClickEvent={handleHeaderEvent} />

        {/* 对象形式的样式编辑 */}
        <Collapse accordion destroyInactivePanel size='middle' items={items} />


      </Form>

      {/* 字符串形式的样式编辑 */}
      <CssEdit value={template2.css} open={open} onValueChange={cssChange} setOpen={setOpen} ref={cssEditRef} />

    </>

  )
}
export default React.memo(HomeAttr) 